/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meteocal.entities;

import com.github.dvdme.ForecastIOLib.FIODaily;
import com.github.dvdme.ForecastIOLib.FIODataPoint;
import com.github.dvdme.ForecastIOLib.ForecastIO;
import com.winterwell.jgeoplanet.GeoPlanet;
import com.winterwell.jgeoplanet.GeoPlanetException;
import com.winterwell.jgeoplanet.Place;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author Luigi
 */
@Entity
public class WeatherWrapper_AsEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    /*necessary attributes
    private Double latitude;
    private Double longitude;
    private String city; 
    private ForecastIO weather;         //This is the real weather object
    private FIODaily daily;             //This is the daily forecast
    
    //Functions that updates every field when a city is inserted
    public void updateFromCity(){
        //Initializing GeoPlanet. GeoPlanet is a utility library for Yahoo! weather services
        //We need it only to do an operation: find the id of a city, having its name
        //(the users don't have the id for each city... stupid users) 
        //UPDATE: considering the forecast, the main purpose of this library is to provide 
        //coordinates from a city
        GeoPlanet client;
        try {
            //A key is needed to connect to Yahoo! Weather services. That's personal and linked to Luigi's Yahoo! account.
            client = new GeoPlanet("2po7Oo_V34FxY_XLnG.cqZlEqst1E2XBdNc5vWd8eVXz7_V9tkm6QB7qrh7T2z3r22FlzmE82iQ-");
        } catch (GeoPlanetException ex) {
            this.city = "NOT ABLE TO CONNECT TO YAHOO WEATHER SERVICES";
            return;
        }
        Place placeInserted;
        try {
            //Getting the GeoPlanet Place of the specified city
            placeInserted = client.getPlace(this.city);
        } catch (GeoPlanetException ex) {
            this.city = "NOT A VALID CITY!";
            return;
        }
        this.latitude = placeInserted.getCentroid().getLatitude();
        this.longitude = placeInserted.getCentroid().getLongitude();
        
        //Now I create the weather object
        //Yes, we're using another library, with the service Forecast.io
        ForecastIO fio = new ForecastIO("7ca983413ba16e2d1d15c95cabce2bb2"); 
        fio.setUnits(ForecastIO.UNITS_SI);             //sets the units as SI - optional
        fio.setExcludeURL("hourly,minutely");             //excluded the minutely and hourly reports from the reply
        fio.getForecast(this.latitude.toString(), this.longitude.toString());
        
        //And finally the weather daily forecast
        this.daily = new FIODaily(fio);
        
    }
    
    //This function give us the daily weather object of a desired day of the month
    public FIODataPoint getSpecificDay(int desiredDay, int currentDay){
        //Check if we have data for that day
        int lastCoveredDay=currentDay+this.daily.days();
        if ((desiredDay<currentDay)||(desiredDay>lastCoveredDay)) return null;
        return daily.getDay(desiredDay-currentDay);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof WeatherWrapper_AsEntity)) {
            return false;
        }
        WeatherWrapper_AsEntity other = (WeatherWrapper_AsEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.meteocal.entities.Weather[ id=" + id + " ]";
    }

    */
    
}
